import 'dart:async';

import 'package:flutter/material.dart';
import 'package:yzb_flutter/model/banner_entity.dart';
import 'package:yzb_flutter/utils/data_utils.dart';
import 'package:yzb_flutter/utils/utils.dart';

class HomeMainPage extends StatefulWidget {
  @override
  _HomeMainPageState createState() => _HomeMainPageState();
}

class _HomeMainPageState extends State<HomeMainPage> {
  List<BannerEntity> bannerList = [];
  List<BannerEntity> showBannerList = [];
  var _pageController = PageController();
  int _currentBannerPage = 1;

  @override
  void initState() {
    super.initState();
    loadBanner();
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      child: Column(
        children: <Widget>[
          buildBanner(),
//          buildList(),
        ],
      ),
    );
  }

  Widget buildBanner() {
    return Container(
      child: PageView.builder(
        itemBuilder: (BuildContext context, int index) {
          BannerEntity be = showBannerList[index];
          return GestureDetector(
            child: Stack(
              children: <Widget>[
                Image.asset(
                  "assets/images/place_holder.png",
                  width: MediaQuery.of(context).size.width,
                  height: MediaQuery.of(context).size.width / 1.8,
                  fit: BoxFit.fill,
                ),
                Image.network(
                  be.imagePath,
                  width: MediaQuery.of(context).size.width,
                  height: MediaQuery.of(context).size.width / 1.8,
                  fit: BoxFit.fill,
                ),
              ],
            ),
            onTap: () {
              Utils.showToast("点击了${be.title}");
            },
          );
        },
        itemCount: showBannerList.length,
        controller: _pageController,
        onPageChanged: _handleBannerChange,
      ),
      width: MediaQuery.of(context).size.width,
      height: MediaQuery.of(context).size.width / 1.8,
    );
  }

  Widget buildList() {
    return Flexible(child:
        ListView.builder(itemBuilder: (BuildContext buildContext, int index) {
      return Text("test$index");
    }));
    return Text("test");
  }

  void loadBanner() {
    DataUtil.getBanner().then((value) {
      if (value != null && (value as List<BannerEntity>).length > 0) {
        List<BannerEntity> tmp = value as List<BannerEntity>;
        setState(() {
          bannerList = value;
          showBannerList = []
            ..add(tmp[tmp.length - 1])
            ..addAll(tmp)
            ..add(tmp[0]);
        });
      }
    });
    Timer.periodic(Duration(seconds: 3), (timer) {
      int addedPosition = (++_currentBannerPage) % showBannerList.length;
      _pageController.animateToPage(addedPosition,
          curve: Curves.linear, duration: Duration(milliseconds: 300));
      if(addedPosition == showBannerList.length-1){
        Future.delayed(Duration(milliseconds: 300),(){
          _pageController.jumpToPage(1);
        });
      }
    });
  }

  void _handleBannerChange(int position) {
    setState(() {
      _currentBannerPage = position;
    });
  }
}
